import signedMerchantInterface from "../../../api/signedMerchant/signedMerchantApi";
import request from "/utils/request"
import { showToast } from "/utils/common";
import { dateFormat } from "/utils/commonMethod"
//引入Promise js文件
const Promise = require('/plugins/es6-promise')
const app = getApp();
Page({
  data: {
    list: [],
    form: {
      merchantType: 'SIGN', // 商户类别属于已签约
      pageSize: 20, // 默认20条
      pageNumber: 1,// 页码
      search: "" // 搜索商户名称
    },
    moreActionModalName: null,
    totalNum: null, // 客户列表总数
    merchantId: null, // 商户id
    currrentClickIndex: null
  },
  onLoad() {
  },
  onShow() {
    // 1-重新设置页码--列表数据
    this.setData({
      "form.pageNumber": 1,
      "form.pageSize": 20,
      "form.search": "",
      list: []
    })
    //2-请求接口
    this.queryPrivateShopList(this.data.form)
  },
  showMoreModal(e) {
    this.setData({
      currrentClickIndex: e.currentTarget.dataset.index,
      moreActionModalName: e.currentTarget.dataset.moreModal,
      merchantId: e.currentTarget.dataset.merchantId
    })
  },
  hideModal() {
    this.setData({
      moreActionModalName: null,
      merchantId: null,
      currrentClickIndex: null
    })
  },
  // 搜索
  onSearch(e) {
    this.setData({
      form: {
        pageSize: 20,
        pageNumber: 1,
        search: e.detail.value
      },
      list: [] //清空列表数据
    })
    this.queryPrivateShopList(this.data.form)
  },
  goViewDetail() {
    dd.navigateTo({
      url: "/pages/signedMerchant/signedMerchantDetail/signedMerchantDetail?merchantId=" + this.data.merchantId
    })
  },
  // 拨打手机号
  goCallPhone(e) {
    if (e.currentTarget.dataset.phoneNumber) {
      dd.showCallMenu({
        phoneNumber: e.currentTarget.dataset.phoneNumber, // 期望拨打的电话号码
        code: '+86', // 国家代号，中国是+86
        showDingCall: true // 是否显示钉钉电话
      });
    }
  },
  // 处理下拉刷新函数
  onPullDownRefresh() {
    setTimeout(() => {
      this.setData({
        "form.pageNumber": 1
      })
      this.queryPrivateShopList(this.data.form)
      dd.stopPullDownRefresh(); //停止下拉刷新
    }, 1000)
  },
  // 页面被拉到底部
  onReachBottom() {
    this.setData({
      'form.pageNumber': ++this.data.form.pageNumber
    })
    this.data.list.length === this.data.totalNum ? '' : this.queryPrivateShopList(this.data.form)
  },
  queryPrivateShopList(param) {
    dd.showLoading({
      content: "加载中..."
    })
    request({
      url: signedMerchantInterface.getSignedShopList,
      method: "get",
      data: param
    }).then((res) => {
      if (res.success) {
        // 处理tags标签 和日期格式
        res.voList.map((item) => {
          item.tags = item.tags ? item.tags.split("&&") : [];
          item.lastVisitTime = dateFormat(item.lastVisitTime);// 处理日期格式展示
        })
        //分页
        if (this.data.form.pageNumber === 1) {
          this.setData({
            list: res.voList,
            totalNum: res.totalNum
          })
        } else {
          this.setData({
            list: [...this.data.list, ...res.voList]
          })
        }
        dd.hideLoading()
      } else {
        dd.alert({
          content: res.messages[0]
        })
      }
    }).finally(() => {
      this.hideModal();
      dd.hideLoading()
    })
  }
});
